package com.fairysunny.reactive;

import java.util.*;
import java.util.function.Supplier;

public class Main {
    public static void main(String[] args) {
        System.out.println("hello");
        ReactiveContext ctx = new ReactiveContext();
        IRef<Integer> i = ctx.ref(1);
        Supplier<String> s = ctx.computed(() -> {
            System.out.println("compute");
            return "i = " + i.get().toString();
        });
        ctx.watch(i, (newValue, oldValue) -> System.out.println("i: " + oldValue + " -> " + newValue), true);
        i.set(2);
        i.set(3);
        i.set(3);
        System.out.println(s.get());
        System.out.println(s.get());
        List<Integer> l = ctx.reactiveList(new ArrayList<>(List.of(1, 2, 3)));
        Supplier<Integer> lSum = ctx.computed(() -> l.stream().mapToInt(v -> v).sum());
        ctx.effect(() -> System.out.println("sum(l) = " + lSum.get()), null, false);
        l.set(1, 2);
        l.set(0, 100);
        l.add(200);
        l.remove(1);
        Map<String, Integer> m = ctx.reactiveMap(new HashMap<>());
        ctx.watch(() -> m.get("hello"), (newValue, oldValue) -> System.out.println("m.hello: " + oldValue + " -> " + newValue), false);
        ctx.watch(m::size, (newValue, oldValue) -> System.out.println("#m: " + oldValue + " -> " + newValue), false);
        m.put("world", 100);
        m.put("hello", 200);
        m.put("world", 300);
        m.put("hello", 400);
        m.put("hello", 400);
        m.remove("hello");
        m.remove("world");
        interface ITest {
            @Getter("a")
            int getA();
            @Setter("a")
            void setA(int a);
        }
        ITest test = ctx.reactiveObject(ITest.class);
        test.setA(100);
        ctx.effect(() -> System.out.println("test.a = " + test.getA()), null, false);
        test.setA(200);
        test.setA(300);
    }
}
